package com.un.ebs.inventory.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.un.ebs.core.util.StringUtils;
import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.inventory.domain.*;
import com.un.ebs.inventory.service.InventoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liubo
 * @since 2020-05-23
 */
@RestController
@RequestMapping("/inventory")
@Api(value = "inventory")
public class InventoryController {

    @Resource
    private InventoryService service;

    @GetMapping("/{id}")
    @ApiOperation(value = "详情", notes = "根据主键获取单个 Inventory")
    public WebResult getById(@PathVariable("id") String id) {
        return WebUtils.success(service.queryById(id));
    }

    @GetMapping("list")
    @ApiOperation(value = "查询库存", notes = "")
    public WebResult<IPage<InventoryExt>> list(@RequestParam(required = false) String searchText, @RequestParam(required = false) String classId, @RequestParam(required = false) String warehouseId, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.query(StringUtils.isNullOrEmpty(searchText) ? null : searchText.trim(), classId, warehouseId, pageSize, pageIndex));
    }

    @GetMapping("listBySku")
    @ApiOperation(value = "查询库存", notes = "")
    public WebResult<List<InventoryExt>> listBySku(@RequestParam String skuId) {
        return WebUtils.success(service.queryBySku(skuId));
    }

    @GetMapping("listJxcByWarehouse")
    @ApiOperation(value = "查询进销存，按仓库", notes = "")
    public WebResult<IPage<InventoryJxcByWarehouse>> listJxcByWarehouse(@RequestParam int year, @RequestParam int month) {
        return WebUtils.success(service.queryJxcByWarehouse(year, month, -1, 1));
    }

    @GetMapping("listJxc")
    @ApiOperation(value = "查询进销存，按SKU", notes = "")
    public WebResult<IPage<InventoryJxcBySku>> listJxc(@RequestParam(required = false) String skuCode, @RequestParam(required = false) String warehouseId, @RequestParam int year, @RequestParam int month, @RequestParam int pageSize, @RequestParam int pageIndex) {
        return WebUtils.success(service.queryJxcBySku(skuCode, warehouseId, year, month, pageSize, pageIndex));
    }

    @GetMapping("listInventoryAge")
    @ApiOperation(value = "查询库齡", notes = "")
    public WebResult<IPage<InventoryAge>> listInventoryAge(@RequestParam int pageSize, @RequestParam int pageIndex) {
        return WebUtils.success(service.queryInventoryAge(pageSize, pageIndex));
    }

//    @GetMapping("listJxc")
//    @ApiOperation(value = "查询进销存，按SKU", notes = "")
//    public WebResult<IPage<JxcVo>> listJxc(@RequestParam(required = false) String skuCode, @RequestParam int pageSize, @RequestParam int pageIndex) {
//        return WebUtils.success(service.queryCurrentJxc(skuCode, pageSize, pageIndex));
//    }

    @GetMapping("listLog")
    @ApiOperation(value = "查询库存日志", notes = "")
    public WebResult<IPage<InventoryLogExt>> listLog(@RequestParam String skuId, @RequestParam String warehouseId, @RequestParam(required = false) String noteCode, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryLog(skuId, warehouseId, noteCode, pageSize, pageIndex));
    }

    @PostMapping("reset")
    @ApiOperation(value = "reset")
    public WebResult reset(@RequestParam(required = false) String skuId) {
        if (StringUtils.isNullOrEmpty(skuId)) {
            for (InventoryExt item : service.query(null, null, null, 999, 1).getRecords()) {
                service.reset(item.getSkuId());
            }
        } else {
            service.reset(skuId);
        }
        return WebUtils.success();
    }

    @PostMapping("updateEndCostMoney")
    @ApiOperation(value = "期末成本调整")
    public WebResult updateEndCostMoney(@RequestParam String id, @RequestParam Double adjustMoney, @RequestParam(required = false) String remark) {
        service.updateEndCostMoney(id, adjustMoney, remark);
        return WebUtils.success();
    }

    @PostMapping("clearEndAdjustMoney")
    @ApiOperation(value = "清除期末成本调整")
    public WebResult clearEndCostMoney(@RequestParam String id) {
        service.updateEndCostMoneyClear(id);
        return WebUtils.success();
    }

    @PostMapping("updateAccount")
    @ApiOperation(value = "更新账务")
    public WebResult updateAccount(@RequestParam int year, @RequestParam int month) {
        try{
            service.updateAccount(year, month);
        }catch (Exception e){
            if(e.getCause().getClass()==SQLException.class){
                SQLException sqlException = (SQLException)e.getCause();
                return WebUtils.error(sqlException.getErrorCode(),sqlException.getMessage());
            }
            return WebUtils.error(e);
        }
        return WebUtils.success();

    }

    @PostMapping("closeAccount")
    @ApiOperation(value = "关账")
    public WebResult closeAccount(@RequestParam int year, @RequestParam int month) {
        try{
            service.updateAccount(year, month);
        }catch (Exception e){
            if(e.getCause().getClass()==SQLException.class){
                SQLException sqlException = (SQLException)e.getCause();
                return WebUtils.error(sqlException.getErrorCode(),sqlException.getMessage());
            }
            return WebUtils.error(e);
        }
        service.closeAccount(year, month);
        return WebUtils.success();
    }

    @PostMapping("closeCancelAccount")
    @ApiOperation(value = "撤销关账")
    public WebResult closeCancelAccount(@RequestParam int year, @RequestParam int month) {
        service.closeCancelAccount(year, month);
        return WebUtils.success();
    }

    @PostMapping("syncFinance")
    @ApiOperation(value = "账务同步")
    public WebResult syncFinance(@RequestParam String accountId) {
        service.syncFinance(accountId);
        return WebUtils.success();
    }

    @GetMapping("listAccountMonth")
    @ApiOperation(value = "会计期间", notes = "")
    public WebResult<IPage<AccountMonth>> queryAccountMonth(@RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryAccountMonth(pageSize, pageIndex));
    }

}